# -*- coding: utf-8 -*-
# @File: backend => gameplay_api.py
# @CreateTime: 2025/8/28 11:21
# @Author: EvanZhang
# @Info: 游戏相关API接口的实现

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import JSONResponse
from typing import List

from apps.crud.gameplay_view import start_game, player_action, get_game_state, advance_game_stage, end_hand
from apps.schemas.gameplay_schema import (
    StartGameRequest, StartGameResponse, 
    PlayerActionRequest, PlayerActionResponse,
    GameStateResponse
)
from apps.schemas.user_schema import UserBase
from apps.utils.jwt import verify_jwt_token

gameplay = APIRouter(prefix='/gameplay', tags=['游戏相关'])

@gameplay.post('/{room_id}/start', summary='开始游戏', response_model=StartGameResponse)
async def start(room_id: int, request: StartGameRequest, current_user: UserBase = Depends(verify_jwt_token)):
    """
    开始游戏
    
    Args:
        room_id: 房间ID
        request: 开始游戏请求
        current_user: 当前用户
    
    Returns:
        StartGameResponse: 开始游戏响应
    """
    result = await start_game(room_id, current_user.id)
    
    if result[0] == 200:
        return StartGameResponse(**result[1])
    else:
        raise HTTPException(status_code=result[0], detail='开始游戏失败: ' + str(result[1]))

@gameplay.post('/{room_id}/action', summary='玩家行动', response_model=PlayerActionResponse)
async def action(room_id: int, request: PlayerActionRequest, current_user: UserBase = Depends(verify_jwt_token)):
    """
    玩家行动
    
    Args:
        room_id: 房间ID
        request: 玩家行动请求
        current_user: 当前用户
    
    Returns:
        PlayerActionResponse: 玩家行动响应
    """
    result = await player_action(room_id, current_user.id, request.action_type, request.amount)
    
    if result[0] == 200:
        return PlayerActionResponse(**result[1])
    else:
        raise HTTPException(status_code=result[0], detail='玩家行动失败: ' + str(result[1]))

@gameplay.get('/{room_id}/state', summary='获取游戏状态', response_model=GameStateResponse)
async def get_state(room_id: int, current_user: UserBase = Depends(verify_jwt_token)):
    """
    获取游戏状态
    
    Args:
        room_id: 房间ID
        current_user: 当前用户
    
    Returns:
        GameStateResponse: 游戏状态响应
    """
    result = await get_game_state(room_id)
    
    if result[0] == 200:
        return GameStateResponse(**result[1])
    else:
        raise HTTPException(status_code=result[0], detail='获取游戏状态失败: ' + str(result[1]))

@gameplay.post('/{room_id}/advance', summary='推进游戏阶段')
async def advance_stage(room_id: int, current_user: UserBase = Depends(verify_jwt_token)):
    """
    推进游戏阶段（发公共牌）
    
    Args:
        room_id: 房间ID
        current_user: 当前用户
    
    Returns:
        dict: 推进结果
    """
    result = await advance_game_stage(room_id)
    
    if result[0] == 200:
        return result[1]
    else:
        raise HTTPException(status_code=result[0], detail='推进游戏阶段失败: ' + str(result[1]))

@gameplay.post('/{room_id}/end', summary='结束牌局')
async def end_game(room_id: int, current_user: UserBase = Depends(verify_jwt_token)):
    """
    结束当前牌局并进行结算
    
    Args:
        room_id: 房间ID
        current_user: 当前用户
    
    Returns:
        dict: 结算结果
    """
    result = await end_hand(room_id)
    
    if result[0] == 200:
        return result[1]
    else:
        raise HTTPException(status_code=result[0], detail='结束牌局失败: ' + str(result[1]))
